package com.jl.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jl.commons.base.BaseController;
import com.jl.commons.constant.Constants;
import com.jl.commons.constant.ErrorCode;
import com.jl.commons.constant.ErrorMessage;
import com.jl.commons.result.PageDataInfo;
import com.jl.commons.result.ResultData;
import com.jl.commons.utils.BASE64;
import com.jl.model.KeyWord;
import com.jl.service.IKeyWordService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 故障关键词表 前端控制器
 * </p>
 *
 * @author JiangJian
 * @since 2019-03-06
 */
@Controller
@RequestMapping("/keyWord")
public class KeyWordController extends BaseController {

    @Autowired
    private IKeyWordService keyWordService;

    @GetMapping("/keyWordDataGrid")
    @RequiresPermissions("/keyWord/keyWordDataGrid")
    public String keyWordList(){
        return "keyWord_list";
    }


    @GetMapping("/add")
    @RequiresPermissions("/keyWord/add")
    public String addKeyWordPage(ModelMap mp, Long id){
        mp.put("id",id);
        return "keyWord_add";
    }

    /**
     * 一定是add
     * @param json
     * @return
     */
    @PostMapping("/add")
    @RequiresPermissions("/keyWord/add")
    @ResponseBody
    public ResultData addKeyWord(String json){
        String value = BASE64.decode(json);
        if(null == value){
            return fail(ErrorCode.BASE64_DECODER_ERROR, ErrorMessage.BASE64_DECODER_ERROR);
        }
        KeyWord keyWord = JSON.parseObject(value,KeyWord.class);

        //判断名称是否重复，不能拥有重复名称
        if(keyWordService.isKeyWordUsed(keyWord.getName())){
            return fail(ErrorCode.KEY_WORD_NAME_USED, ErrorMessage.KEY_WORD_NAME_USED);
        }
        keyWord.setCreateId(getUserId());
        keyWord.setGmtCreate(new Date());

        keyWordService.save(keyWord);
        return success(keyWord);
    }


    @GetMapping("/edit")
    @RequiresPermissions("/keyWord/edit")
    public String editKeyWordPage(ModelMap mp, Long id){
        mp.put("id",id);
        return "keyWord_add";
    }

    /**
     * 一定是edit
     * @param json
     * @return
     */
    @PostMapping("/edit")
    @RequiresPermissions("/keyWord/edit")
    @ResponseBody
    public ResultData editKeyWord(String json){
        String value = BASE64.decode(json);
        if(null == value){
            return fail(ErrorCode.BASE64_DECODER_ERROR, ErrorMessage.BASE64_DECODER_ERROR);
        }
        KeyWord keyWord = JSON.parseObject(value,KeyWord.class);

        //id存在表示是更新数据

        //判断编号是不是自己的，如果是自己的则表示可以修改
        boolean canUse = keyWordService.isKeyWordCanUse(keyWord.getName(),keyWord.getId());
        if(!canUse){
            return fail(ErrorCode.DEVICE_NUMBER_USED,ErrorMessage.DEVICE_NUMBER_USED);
        }
        keyWord.setGmtModified(new Date());
        keyWord.setModifyId(getUserId());

        keyWordService.updateById(keyWord);
        return success(keyWord);
    }

    @PostMapping("/delete")
    @RequiresPermissions("/keyWord/delete")
    @ResponseBody
    public ResultData deletesKeyWord(String json){
        String value = BASE64.decode(json);
        if(null == value){
            return fail(ErrorCode.BASE64_DECODER_ERROR, ErrorMessage.BASE64_DECODER_ERROR);
        }
        List<Long> ids = JSONArray.parseArray(value,Long.class);
        boolean b = keyWordService.removeByIds(ids);
        if(b){
            return success();
        }
        return fail();
    }

    /**
     * 一定是dataGrid
     * @param current
     * @param pageSize
     * @param searchCol
     * @param searchValue
     * @return
     */
    @PostMapping("/dataGrid")
    @RequiresPermissions("/keyWord/dataGrid")
    @ResponseBody
    public ResultData keyWordDataGrid(Long current,Long pageSize,String searchCol,String searchValue){
        if(current == null || pageSize == null){
            return fail(ErrorCode.PAGE_PARAM_ERROR, ErrorMessage.PAGE_PARAM_ERROR);
        }
        QueryWrapper<KeyWord> queryWrapper = new QueryWrapper<>();
        //目前的查询只有单表
        if(StringUtils.isNotBlank(searchValue) && StringUtils.isNotBlank(searchCol)){
            if(searchCol.equals(Constants.ID)){
                queryWrapper.eq("t.id",searchValue);
            }else{
                queryWrapper.like("t."+searchCol,searchValue);
            }
        }
        PageDataInfo<Map<String,Object>> pageDataInfo =new PageDataInfo<>();
        pageDataInfo.setCurrent(current);
        pageDataInfo.setSize(pageSize);

        keyWordService.selectKeyWordDataGrid(pageDataInfo,queryWrapper);
        return success(pageDataInfo);
    }

}
